package com.example.whopxx.dao;

import com.example.whopxx.domain.UserBean;
import com.example.whopxx.utils.JDBCUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserDao {
    public boolean updateUser(UserBean user){
        Connection connection = null;
        PreparedStatement prestmt = null;
        try {
            connection = JDBCUtils.getConnection();
            String sql = "update userinfo set username=?,password=?,truename=?,phone=?,gender=?,email=?,card=?,cardNo=? where id=?";
            prestmt = connection.prepareStatement(sql);
            prestmt.setString(1,user.getUsername());
            prestmt.setString(2,user.getPassword());
            prestmt.setString(3,user.getTruename());
            prestmt.setString(4,user.getPhone());
            prestmt.setString(5,user.getGender());
            prestmt.setString(6,user.getEmail());
            prestmt.setString(7,user.getCard());
            prestmt.setString(8,user.getCardNo());
            prestmt.setInt(9,user.getId());
            return prestmt.executeUpdate() > 0;
        } catch (ClassNotFoundException e){
            throw new RuntimeException(e);
        } catch (SQLException e) {
        throw new RuntimeException(e);
        } finally {
            JDBCUtils.release(prestmt, connection);
        }
    }
    public boolean deleteUserById(String id){
        Connection connection = null;
        PreparedStatement prestmt = null;
        try {
            connection = JDBCUtils.getConnection();
            String sql = "delete from userinfo where id=?";
            prestmt = connection.prepareStatement(sql);
            prestmt.setString(1,id);
            return prestmt.executeUpdate() > 0;
        } catch (ClassNotFoundException e){
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtils.release(prestmt, connection);
        }
    }
    public List<UserBean> findAll() {
        Connection connection = null;
        Statement statement = null;
        try {
            connection = JDBCUtils.getConnection();
            statement = connection.createStatement();
            String sql = "select * from userinfo";
            ResultSet rs = statement.executeQuery(sql);
            List<UserBean> list = new ArrayList<>();
            while(rs.next()){
                UserBean user = new UserBean();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setTruename(rs.getString("truename"));
                user.setPhone(rs.getString("phone"));
                user.setGender(rs.getString("gender"));
                user.setEmail(rs.getString("email"));
                user.setCard(rs.getString("card"));
                user.setCardNo(rs.getString("cardNo"));
                list.add(user);
            }
            return list;
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtils.release(statement, connection);
        }
    }

    public UserBean findUserById(String id) {
        Connection connection = null;
        Statement statement = null;
        try {
            connection = JDBCUtils.getConnection();
            statement = connection.createStatement();
            String sql = "select * from userinfo where id ='" + id + "'";
            ResultSet rs = statement.executeQuery(sql);
            UserBean user = new UserBean();
            while(rs.next()){
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setTruename(rs.getString("truename"));
                user.setPhone(rs.getString("phone"));
                user.setGender(rs.getString("gender"));
                user.setEmail(rs.getString("email"));
                user.setCard(rs.getString("card"));
                user.setCardNo(rs.getString("cardNo"));
            }
            return user;
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtils.release(statement, connection);
        }
    }
    public boolean findUser(String username, String password) {
        Connection conn = null;
        Statement stmt = null;
        try {
            conn = JDBCUtils.getConnection();

            stmt = conn.createStatement();

            String sql = "select * from userinfo where username='" + username + "' and password='" + password + "'";

            ResultSet rs = stmt.executeQuery(sql);

            if (rs.next()) {
                return true;
            }
            return false;

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtils.release(stmt, conn);
        }
    }
    public boolean insert(UserBean userBean){
        Connection connection = null;
        PreparedStatement prestmt = null;
        try {
            connection = JDBCUtils.getConnection();
            String sql = "insert into userinfo(username,password,truename,phone,gender,email,card,cardNo) values(?,?,?,?,?,?,?,?)";
            prestmt = connection.prepareStatement(sql);
            prestmt.setString(1,userBean.getUsername());
            prestmt.setString(2,userBean.getPassword());
            prestmt.setString(3,userBean.getTruename());
            prestmt.setString(4,userBean.getPhone());
            prestmt.setString(5,userBean.getGender());
            prestmt.setString(6,userBean.getEmail());
            prestmt.setString(7,userBean.getCard());
            prestmt.setString(8,userBean.getCardNo());
            return prestmt.executeUpdate() > 0;
        } catch (ClassNotFoundException e){
            throw new RuntimeException(e);
        } catch (SQLException e){
            throw new RuntimeException(e);
        } finally {
            JDBCUtils.release(prestmt, connection);
        }
    }
}
